;; TODO: FIX ME  /soegaard 27 jun 2005

;;; stack.ss  --  Jens Axel Soegaard  --  2003

(require (lib "plt-pretty-big-text.ss" "lang"))

;;; STACKS

; The Stack Signature
(define-signature stack^    
  (empty
   empty?
   insert
   remove-first
   first))

;;; USAGE
; To use a specific implementation use use-stack to export
; the names to the current namespace. 
; 
;     (use-stack list-stack@)
;  or (use-stack list-stack@ prefix)


;;; IMPLEMENTATION USING BUILTIN LISTS

(define list-stack@
  ; All operations except last runs in O(1).
  ; The time for last is O(n), where n is the number of elements in the stack.
  
  (unit/sig stack^ 
    (import)
  
    (define empty '())
    (define empty? null?)
    (define (insert stack element)
      (cons element stack))
    (define remove-first cdr)
    (define first car)))

(define-syntax use-stack
  ; TODO: I wanted to use 
  ;           (define-values/invoke-unit/sig stack^ list-stack@ s)
  ;       but it did not work, hence the use of namespace-variable-bind/invoke-unit/sig.
  (syntax-rules ()
    [(use-stack stack-unit)        (namespace-variable-bind/invoke-unit/sig stack^ stack-unit)]
    [(use-stack stack-unit prefix) (namespace-variable-bind/invoke-unit/sig stack^ stack-unit prefix)]))

